package com.atgion.manager.controller;


import com.atgion.manager.service.PictureCategoryService;
import com.atgion.spzx.model.entity.ImageManagement.PictureCategory;
import com.atgion.spzx.model.vo.base.Result;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.atgion.exception.exception.GlobalException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


//图片分类
@Slf4j
@Tag(name = "SysMenuController", description = "菜单信息控制层")
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/picture")
public class PictureCategoryController {

    //    图片分类
    private final PictureCategoryService pictureCategoryService;

    @Operation(summary = "根据parentId获取下级节点")
    @GetMapping("/findByParentId/{categoryId}")
    public Result<List<PictureCategory>> findByParentId(@PathVariable Long categoryId) {
        List<PictureCategory> list = pictureCategoryService.findByParentId(categoryId);
        return Result.success(list) ;
    }

    @Operation(summary = "获取所有图片分类")
    @GetMapping("/findAll")
    public Result<List<PictureCategory>> findAll() {
        List<PictureCategory> list = pictureCategoryService.lambdaQuery()
                .eq(PictureCategory::getCategoryId, 0)
                .eq(PictureCategory::getIsDeleted,0)
                .orderByAsc(PictureCategory::getOrderNum)
                .list();
        if(!CollectionUtils.isEmpty(list)) {
            // 遍历分类的集合，获取每一个分类数据
            list.forEach(category -> {
                category.setHasChildren(pictureCategoryService.lambdaQuery().eq(PictureCategory::getCategoryId,category.getId()).list().size()>0);
            });
        }
        return Result.success(list) ;
    }

    @Operation(summary = "新增图片分类")
    @PostMapping("/save")
    public Result<Void> save(@RequestBody PictureCategory pictureCategory) {
        Long categoryId = pictureCategory.getCategoryId();
        pictureCategory.setCategoryId(categoryId != null && StringUtils.isNotBlank(categoryId.toString()) ? categoryId : 0);

//       获取 当前事件转为字符串

        pictureCategory.setUpdateTime(LocalDateTime.now());
        pictureCategory.setCreateTime(LocalDateTime.now());
        pictureCategory.setIsDeleted(0);



        try {
            pictureCategoryService.save(pictureCategory);
        } catch (Exception e) {
            throw new GlobalException("300","已有分类名称,请重启取名");
        }

        return Result.success();
    }
    //    修改图片分类
    @Operation(summary = "修改图片分类")
    @PutMapping("/update")
    public Result<Void> update(@RequestBody PictureCategory pictureCategory) {
        if(pictureCategory.getCategoryId()==pictureCategory.getId()) return Result.failed("不能将父类修改成自己") ;
        pictureCategory.setCategoryId(pictureCategory.getCategoryId()!=null?pictureCategory.getCategoryId():0);
        pictureCategoryService.updateById(pictureCategory);
        return Result.success() ;
    }
    //    删除图片分类
    @Operation(summary = "删除图片分类")
    @DeleteMapping("/remove/{id}")
    public Result<Void> remove(@PathVariable Long id) {
        List<PictureCategory> list = pictureCategoryService.lambdaQuery()
                .eq(PictureCategory::getCategoryId, id)
                .eq(PictureCategory::getIsDeleted,0)
                .list();
        if(list.size()>0){
            return Result.success() ;
        }
        pictureCategoryService.removeById(id);
        return Result.success() ;
    }

}
